package com.slipper.system.collect.service;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.system.schedule.bo.ScheduleBO;
import com.slipper.system.schedule.service.ScheduleService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.collect.dao.CollectTaskDao;
import com.slipper.system.collect.bo.CollectTaskBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;

@Service
@ReadTransactional
public class CollectTaskService {
    @Resource
    private CollectTaskDao collectTaskDao;
    @Resource
    private CollectTableService collectTableService;
    @Resource
    private ScheduleService scheduleService;
    
    public CollectTaskBO queryBy(String id) {
        return collectTaskDao.queryBy(id);
    }
    
    public List<CollectTaskBO> queryList(CollectTaskBO bo) {
        return collectTaskDao.queryList(bo);
    }
    
    public PageInfo<CollectTaskBO> queryPage(CollectTaskBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(collectTaskDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(CollectTaskBO bo) {
        int result = 0;
        if (Tool.isNotBlank(bo.getId())) {
            bo.setUpdateTime(System.currentTimeMillis());
            result += collectTaskDao.update(bo);
        } else {
            bo.setId(UUID.getUUID());
            bo.setUpdateTime(System.currentTimeMillis());
            bo.setCreateTime(System.currentTimeMillis());
            result += collectTaskDao.insert(bo);
        }

        result += collectTableService.save(bo.getId(), bo.getTableList(), bo.getUpdateId());

        ScheduleBO schedule = new ScheduleBO();
        schedule.setType(bo.getSyncType());
        schedule.setPeriod(bo.getSyncPeriod());
        schedule.setTime(bo.getSyncTime());
        schedule.setExpression(bo.getSyncExpression());
        schedule.setBusinessType(ScheduleBO.BUSINESS_COLLECT);
        schedule.setBusinessId(bo.getId());
        schedule.setStatus(bo.getStatus());

        if (CollectTaskBO.SYNC_MODEL_MANUALLY.equals(bo.getSyncMode())) {
            // 改变成手动，删除原有的调度任务
            scheduleService.deleteByBusiness(schedule);
        } else {
            scheduleService.updateByBusiness(schedule);
        }

        return result;
    }
    
    @WriteTransactional
    public int delete(CollectTaskBO bo) {
        int result = 0;
        if (bo.getIds() != null) {
            for (String id : bo.getIds()) {
                result += deleteById(id);
            }
        }
        if (Tool.isNotBlank(bo.getId())) {
            result += deleteById(bo.getId());
        }
        return result;
    }

    @WriteTransactional
    public int deleteById(String id) {
        collectTableService.deleteByTaskId(id);

        CollectTaskBO bo = queryBy(id);
        ScheduleBO schedule = new ScheduleBO();
        schedule.setBusinessType(ScheduleBO.BUSINESS_COLLECT);
        schedule.setBusinessId(bo.getId());
        scheduleService.deleteByBusiness(schedule);

        return collectTaskDao.delete(bo);
    }
}
